﻿
@{
    ViewBag.Title = "Danh sách lớp tín chỉ";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery.b4byb0o.js"></script>
<div class="row-fluid">
    <div class="span12">
        <form class="form-horizontal">
            <div class="span4">
                <div class="control-group">
                    <label class="control-label">
                        Chọn kỳ đăng ký: 
                    </label>
                    <div class="controls">
                        @(Html.Kendo().DropDownList()
                            .OptionLabel("Tất cả")
                            .Name("KyDangKy")
                            .DataTextField("Text")
                            .DataValueField("Value")
                            .DataSource(source =>
                            {
                                source.Read("getKyDangKy", "DanhSachLopTC")
                                ;
                            })
    
                            .AutoBind(true)
                        )
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label">
                        Chọn hệ đào tạo: 
                    </label>
                    <div class="controls">
                        @(Html.Kendo().DropDownList()
                        //.OptionLabel("Tất cả")
                        .Name("He")
                        .DataTextField("Text")
                        .DataValueField("Value")
                        .HtmlAttributes(new { style = "width: 200px" })
                        .DataSource(source =>
                        {
                            source.Read(read =>
                            {
                                read.Action("getHeDaoTao", "DanhSachLopTC").Data("getFilters");
                            })
                            .ServerFiltering(true)
                            ;
                        })
                        .CascadeFrom("KyDangKy")
                        //.AutoBind(false)
                        .Events(e =>
                        {
                            e.DataBound("He_DataBound");
                        })
                    )
                        <script>
                            function He_DataBound() {
                                $('#He').data('kendoDropDownList').value(0);
                            }
                        </script>
                    </div>
                </div>
            </div>
            <div class="span4">
                <div class="control-group">
                    <label class="control-label">
                        Chọn chuyên ngành: 
                    </label>
                    <div class="controls">
                        @(Html.Kendo().DropDownList()
                            //.OptionLabel("Tất cả")
                            .Name("ChuyenNganh")
                            .DataTextField("Text")
                            .DataValueField("Value")
                            .HtmlAttributes(new { style = "width: 200px" })
                            .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("getChuyenNganh", "DanhSachLopTC").Data("getFilters");
                                })
                                .ServerFiltering(true)
                                ;
                            })
                            .CascadeFrom("He")
                            //.AutoBind(false)
                            .Events(e =>
                            {
                                e.DataBound("ChuyenNganh_DataBound");
                            })
                        )
                        <script>
                            function ChuyenNganh_DataBound() {
                                $('#ChuyenNganh').data('kendoDropDownList').value(0);
                            }
                        </script>
                    </div>
                </div>
            
                <div class="control-group">
                    <label class="control-label">
                        Chọn môn tín chỉ:
                    </label>
                    <div class="controls">
                        @(Html.Kendo().DropDownList()
                            .OptionLabel("Tất cả")
                            .Name("MonTC")
                            .DataTextField("Text")
                            .DataValueField("Value")
                            .HtmlAttributes(new { style = "width:300px" })
                            .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("getMonTC", "DanhSachLopTC").Data("getFilters");
                                })
                                .ServerFiltering(true)
                                ;
                            })
                            .AutoBind(false)
                            .CascadeFrom("ChuyenNganh")
                            .Events(e=>{
                                e.DataBound("MonTC_DataBound");
                                e.Change("loadGridLopTC");
                            })
                        )
                        <script>
                            function MonTC_DataBound() {
                                $('#MonTC').data('kendoDropDownList').value(0);
                                loadGridLopTC();
                            }
                        </script>
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label">
                    </label>
                    <div class="controls">
                        <label class="checkbox"><input type="checkbox"" id="Co_SKTC"/> Lớp đã được xếp lịch học</label>
                    </div>
                </div>
            </div>
            <div class="span4">
                <div class="control-group">
                    <label class="control-label" >Số lớp TC: <span id="lbSoLopTC"></span></label>
                </div>
                <div class="control-group">
                    <label class="control-label" >Số SV đăng ký: <span id="lbSoSV"></span></label>
                </div>
            </div>
            <div class="span12">
                <p>
                    <a href="javascript:;" class="btnDuyetSelected btn purple" data-action="DuyetDKs">Duyệt</a> &nbsp;
                    <a href="javascript:;" class="btnDuyetSelected btn" data-action="BoDuyetDKs">Bỏ duyệt</a>&nbsp;
                    <a href="javascript:;" class="btn blue btnChuyenLop">Chuyển lớp</a>&nbsp;
                    <a href="@Url.Action("HuyLopTC", "DanhSachLopTC", new { Area="Admin"})" class="btn red btnHuyLop">Hủy lớp</a>&nbsp;
                    <a href="@Url.Action("RutHocPhan", "DanhSachLopTC", new { Area="Admin"})" class="btn btnRutHocPhan">Rút học phần</a>
                    <a href="@Url.Action("HuyHocPhan", "DanhSachLopTC", new { Area="Admin"})" class="btn btnHuyHocPhan">Hủy học phần</a>
                    <a href="#modalLopTC" id="modalChuyenLop" data-toggle="modal" style="display:none"></a>
                </p>
            </div>
        </form>
        
    </div>
    <div class="span12">
        <div style="overflow:hidden">
            <div class="span7">
                @(Html.Kendo().Grid<StudentPortal.ViewModels.LopTinChiViewModel>()
                    .Name("GridLopTC")
                    .Columns(column =>
                    {
                        column.Template(@<text></text>)
                            .HeaderTemplate("<input type='checkbox' class='checkAllLopTC' />")
                            .ClientTemplate("<input type='checkbox' class='checkRowLopTC' value='#=ID_lop_tc#' />").Width(30);
                        column.Bound(t=>t.Ma_mon).Width(80);
                        column.Bound(t => t.Ten_lop_tc);
                        column.Bound(t => t.Giang_vien).Width(80);
                        column.Bound(t => t.Chi_tiet).Width(80);
                        column.Bound(t => t.Da_dang_ky).Width(80);
                        column.Bound(t => t.Cho_trong).Width(80);
                        column.Template(@<text></text>).ClientTemplate(@"#
var color ='';
if(Cho_trong == 0) color='row-red';
if(Da_dang_ky == 0) color='row-red';
#
<div class='changeRowColor' row-color='#=color#'></div>").Hidden();
                    })
                        .DataSource(source =>
                        {
                            source.Ajax().Read(read =>
                            {
                                read.Action("getLopTC", "DanhSachLopTC").Data("getFilters");
                            }).PageSize(50);
                        })
                        .Groupable()
                        .Pageable()
                        .Selectable()
                        .Sortable()
                        .Scrollable(scroll=>scroll.Height(500))
                        .Pageable(pager => pager.PageSizes(new int[] { 50, 100, 200 }))
                        .Filterable()
                        .Events(e =>
                        {
                            e.Change("GridLopTC_change");
                            e.DataBound("GridLopTC_DataBound");
                        })
                    )
            </div>
        
            <div class="span5">
                @(Html.Kendo().Grid<StudentPortal.ViewModels.SinhVienViewModel>()
                    .Name("GridSinhVien")
                    .Columns(column =>
                    {
                        column.Template(@<text></text>)
                            .ClientTemplate(@"<input type='checkbox' value='#=ID_sv#' class='checkRow'/>")
                            .HeaderTemplate(@"<input type='checkbox' class='checkAll'/>")
                            .Width(30)
                            ;
                        column.Bound(t => t.Ma_sv)
                            .Width(60)
                            ;
                        column.Bound(t => t.Ho_ten)
                            ;
                        column.Bound(t => t.Lop)
                            .Width(90)
                            ;
                        column.Template(@<text></text>).ClientTemplate(@"#
                            var label = 'Duyệt';
                            var action = 'DuyetDK';
                            if(Duyet==1)
                            {
                                action = 'BoDuyetDK';
                                label = 'Hủy';
                            }
                            # #if(!Rut_bot_hoc_phan){#<a class='k-button-icontext k-button btnDuyet' href='javascript:;' ID_sv='#=ID_sv#' data-action='#=action#'>#=label#</a>#}#")
                            .Width(90)
                            ;
                        column.Template(@<text></text>).ClientTemplate(@"#
var color ='';
if(Rut_bot_hoc_phan) color='row-red';
#
<div class='changeRowColor' row-color='#=color#'></div>").Hidden();
                    })
                            .DataSource(source =>
                            {
                                source.Ajax().Read(read =>
                                {
                                    read.Action("getSinhVienDK", "DanhSachLopTC").Data("getFilters");
                                }).PageSize(50);
                            }
                            )

                            .Groupable()
                            .Pageable()
                            .Selectable()
                            .Sortable()
                            .Scrollable(scroll => scroll.Height(500))
                            .Pageable(pager => pager.PageSizes(new int[] { 50, 100, 200 })
                                )
                            .Events(e =>
                            {
                                e.DataBound("GridSinhVien_DataBound");
                            })
                            .Filterable()
                        )
                
            </div>
        </div>
        <p></p>
    </div>
</div>

<div id="modalLopTC" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
		<h3 id="myModalLabel1">Chuyển lớp tín chỉ</h3>
	</div>
	<div class="modal-body">
		@(Html.Kendo().DropDownList()
            .Name("DropDownLopTc")
            .OptionLabel("Chọn lớp tín chỉ")
            .DataTextField("Text")
            .DataValueField("Value")

            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("getLopTCKhac", "DanhSachLopTC").Data("getFilters");
                })
                .ServerFiltering(true);
            })
            .AutoBind(false)
            .HtmlAttributes(new { style = "width:300px" })
        )
    </div>
	<div class="modal-footer">
		<button class="btn yellow" id="btnXacNhanChuyen">Chuyển</button>
        <button class="btn" data-dismiss="modal" aria-hidden="true">Đóng</button>
	</div>
</div>
<script type="text/javascript">
    var seletedIdLop = 0;
    
    function GridLopTC_DataBound(e) {
        $('#lbSoLopTC').html(e.sender.dataSource._total);
        updateRowColor();
        $('.checkAllLopTC').updateCheckRow({
            checkRow: '.checkRowLopTC',
            changeElement: '$(".checkAllLopTC").parent()'
        });

    }
    
    function GridLopTC_change(arg) {
        var selected = $.map(this.select(), function (item) {
            return $(item);
        });
        var item = this.dataItem(selected[0]);
        seletedIdLop = item.ID_lop_tc;
        selectedIDs = {};
        $('#GridSinhVien').data("kendoGrid").dataSource.read();
        
    }
    function loadGridLopTC(e)
    {
        $('#GridLopTC').data("kendoGrid").dataSource.read();
    }
    function GridSinhVien_DataBound(e) {
        $('#lbSoSV').html(e.sender.dataSource._total);
        updateRowColor();
        $('.checkAll').updateCheckRow({
            checkRow: '.checkRow',
            changeElement: '$(".checkAll").parent()'
        });
    }
    function object2array(object)
    {
        var IDs = [];
        var i=0;
        for (var id in object)
            if (object[id]) IDs[i++] = id;
        return IDs;
    }
    
    $(document).ready(function () {

        $('.checkAllLopTC').initCheckAll({
            checkRow: '.checkRowLopTC',
            changeElement: '$(".checkAllLopTC").parent()'
        });

        $('.checkAll').initCheckAll({
            checkRow: '.checkRow',
            changeElement: '$(".checkAll").parent()'
        });

        $('.btnHuyLop').ajaxAction({
            data: getFilters,
            success: function (o) {
                alert(o.Message);
                if (o.status == 1) {
                    $('#GridLopTC').data('kendoGrid').dataSource.read();
                }
            }
        });

        $('.btnRutHocPhan,.btnHuyHocPhan').ajaxAction({
            data: function () {
                return {
                    IDs: $('.checkAll').selectedIDsToString(),
                    ID_lop_tc: seletedIdLop,
                }
            },
            success: function (o) {
                alert(o.Message);
                if (o.status == 1) {
                    $('#GridSinhVien').data('kendoGrid').dataSource.read();
                }
            }
        });


        $('#Co_SKTC').change(function (e) {
            loadGridLopTC(e);
        });
        
        $('#btnXacNhanChuyen').click(function () {
            if ($('.checkAll').isSelectedIDsEmpty()) {
                alert("Không có sinh viên nào được chọn!");
                return;
            }
            $.ajax({
                url: '@Url.Action("ChuyenLop", "DanhSachLopTC")',
                 type: 'post',
                 data: {
                     IDs: $('.checkAll').selectedIDsToString(),
                     ID_lop_tc_cu: seletedIdLop,
                     ID_lop_tc: $('#DropDownLopTc').val(),
                 },
                 dataType: 'json',
                 success: function (o) {
                     alert(o.Message);
                     if (o.status == 1)
                         $('#GridSinhVien').data('kendoGrid').dataSource.read();
                 }
             });
        });

        
        $('.btnChuyenLop').click(function () {
            if ($('.checkAll').isSelectedIDsEmpty()) {
                alert("Không có sinh viên nào được chọn");
                return;
            }
            $('#DropDownLopTc').data('kendoDropDownList').dataSource.read();
            $('#modalChuyenLop').click();
        });
        $('.btnDuyetSelected').click(function () {
            if ($('.checkAll').isSelectedIDsEmpty()) {
                alert("Không có sinh viên nào được chọn");
                return;
            }
            IDs = object2array(selectedIDs);
            $.ajax({
                url: '/Admin/DanhSachLopTC/' + $(this).attr('data-action'),
                data: {
                    IDs: IDs.join(),
                    ID_lop_tc: seletedIdLop,
                },
                dataType: 'json',
                success: function (o) {
                    alert(o.Message);
                    $('#GridSinhVien').data("kendoGrid").dataSource.read();

                }
            });
        });
        $('.btnDuyet').live('click',function () {
            $.ajax({
                url: '/Admin/DanhSachLopTC/' + $(this).attr('data-action'),
                dataType: 'json',
                data: {
                    ID_sv: $(this).attr('ID_sv'),
                    ID_lop_tc: seletedIdLop,
                },
                success: function (o) {
                    alert(o.Message);
                    $('#GridSinhVien').data("kendoGrid").dataSource.read();
                }
            });
        });
    })
    function getFilters() {
        return {
            ID_he: $('#He').val(),
            ID_chuyen_nganh: $('#ChuyenNganh').val(),
            Ky_dang_ky: $('#KyDangKy').val(),
            ID_mon: $('#MonTC').val(),
            ID_lop_tc: seletedIdLop,
            Co_SKTC: $('#Co_SKTC').attr('checked')=='checked',
        };
    }
    function Edit(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest('tr'));
        location.href = '@Url.Action("Edit", "DanhSachLopTC")/' + dataItem.ID_lop_tc;
    }
    function Delete(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest('tr'));
        location.href = '@Url.Action("Delete", "DanhSachLopTC")/' + dataItem.ID_lop_tc;
    }
</script>
